package com.rabbit.work.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


@Configuration
public class RabbitConfig {
    @Bean
    public Queue bizQueue() {
        return QueueBuilder.durable("qw.biz").build();
    }

    @Bean
    public Queue ttlQueue() {
        return QueueBuilder.durable("qw.ttl").ttl(10000).deadLetterRoutingKey("rw.dlx").deadLetterExchange("ew.dlx").build();
    }

    @Bean
    public Queue dlxQueue() {
        return QueueBuilder.durable("qw.dlx").build();
    }

    @Bean
    public Exchange bizExchange() {
        return ExchangeBuilder.directExchange("ew.biz").durable(true).build();
    }

    @Bean
    public Exchange ttlExchange() {
        return ExchangeBuilder.directExchange("ew.ttl").durable(true).build();
    }

    @Bean
    public Exchange dlxExchange() {
        return ExchangeBuilder.directExchange("ew.dlx").durable(true).build();
    }

    @Bean
    public Binding bizBinding() {
        return BindingBuilder.bind(bizQueue()).to(bizExchange()).with("rw.biz").noargs();
    }

    @Bean
    public Binding ttlBinding() {
        return BindingBuilder.bind(ttlQueue()).to(ttlExchange()).with("rw.ttl").noargs();
    }

    @Bean
    public Binding dlxBinding() {
        return BindingBuilder.bind(dlxQueue()).to(dlxExchange()).with("rw.dlx").noargs();
    }

    @Bean
    public RabbitAdmin rabbitAdmin(ConnectionFactory factory) {
        return new RabbitAdmin(factory);
    }

    @Bean
    public RabbitTemplate rabbitTemplate(ConnectionFactory factory) {
        return new RabbitTemplate(factory);
    }

    @Bean
    public MessageConverter messageConverter() {
        return new Jackson2JsonMessageConverter();
    }
}
